package com.central.apps.handler.workorder;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class workorder extends LvBaseHandler {


    @Autowired
    private HandlerService handlerService;


    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
//        //首页图表 mttr mtbf  稼动率
//        if("workorderApm".equals(type)){
//            Map<String, Object> resultMap = new HashMap<String, Object>();
//            Map<String, Object> resultMap1 = new HashMap<String, Object>();
//            List<Map<String, Object>> list = handlerService.findBySql
//                    (" select b.deptcode,dept.fullname,con1,con2,ifnull((1/(con1/con2)),0) mtbf,ifnull(mttr,0) mttr,ifnull(jdl,0) jdl from ( " +
//                            " select  wo.deptcode,dept.fullname,sum(downtime) ,count(*) con1  from bl_workorder  wo  " +
//                            " left join sys_dept dept  on wo.deptcode=dept.code " +
////                            "  where isshutdown='停机' " +
//                            " group by wo.deptcode,dept.fullname ) a  " +
//                            " left join ( " +
//                            " select deptcode,count(*) con2 from bl_asset  " +
//                            " group  by deptcode )b on a.deptcode=b.deptcode " +
//                            " left join (select  wo.deptcode,dept.fullname,sum(downtime) ,count(*),(sum(downtime))/(count(*)) mttr,((12*30)-sum(downtime))/sum(downtime) jdl from bl_workorder  wo  " +
//                            " left join sys_dept dept  on wo.deptcode=dept.code " +
////                            " where isshutdown='停机' " +
//                            " group by wo.deptcode,dept.fullname) c on a.deptcode=c.deptcode " +
//                            " left join sys_dept dept on dept.code=b.deptcode");
//            List<Object> resuleList1 = new ArrayList<Object>(); // X轴
//            List<Object> resuleList2 = new ArrayList<Object>(); // Y轴
//            List<Object> resuleList3 = new ArrayList<Object>(); // mttr
//            List<Object> resuleList4 = new ArrayList<Object>(); // mtbf
//            List<Object> resuleList5 = new ArrayList<Object>(); // 稼动率
//
//            Map<String, Object> yMap = new HashMap<String, Object>();
//
//            Map<String, Object> yMap1 = new HashMap<String, Object>();
//            Map<String, Object> yMap2= new HashMap<String, Object>();
//            Map<String, Object> yMap3 = new HashMap<String, Object>();
//            for (int i = 0; i < list.size(); i++) {
//                resuleList1.add(list.get(i).get("fullname"));//x轴  各个分厂的名字
//                resuleList3.add(list.get(i).get("mttr"));
//                resuleList4.add(list.get(i).get("mtbf"));
//                resuleList5.add(list.get(i).get("jdv"));
//            }
//            yMap1.put("data",resuleList3);
//            yMap1.put("name","MTTR");
//            yMap2.put("data",resuleList4);
//            yMap2.put("name","MTBF");
//            yMap3.put("data",resuleList5);
//            yMap3.put("name","稼动率");
//            resuleList2.add(yMap1);
//            resuleList2.add(yMap2);
//            resuleList2.add(yMap3);
//            resultMap.put("msg","操作成功");
//            resultMap.put("code","0");
//            resultMap1.put("xAxis",resuleList1);
//            resultMap1.put("yAxis",resuleList2);
//            resultMap.put("data",resultMap1);
//
//            return resultMap;
//        }
        //首页图表 工单类型，及次数
        if("workorderType".equals(type)){
            Map<String, Object> resultMap = new HashMap<String, Object>();
            Map<String, Object> resultMap1 = new HashMap<String, Object>();
//            List<Map<String, Object>> list = handlerService.findBySql
//                    (" select deptcode,fullname,  " +
//                            " sum(jiben) jiben,  " +
//                            " sum(xunjian) xunjian,  " +
//                            " sum(fuwu) fuwu,  " +
//                            " sum(jianweixiu) jianweixiu,  " +
//                            " sum(weixiu) weixiu,   " +
//                            " sum(weiwai) weiwai   " +
//                            " from (  " +
//                            " select  wo.deptcode,dept.fullname  ,  " +
//                            " case UDWOTYPE when '基本工单' then 1 else 0 end jiben ,  " +
//                            " case UDWOTYPE when '巡点检工单' then 1 else 0 end xunjian ,  " +
//                            " case UDWOTYPE when '服务工单' then 1 else 0 end fuwu ,  " +
//                            " case UDWOTYPE when '检维修工单' then 1 else 0 end jianweixiu ,  " +
//                            " case UDWOTYPE when '维修工单' then 1 else 0 end weixiu ,  " +
//                            " case UDWOTYPE when '委外工单' then 1 else 0 end weiwai   " +
//                            " from bl_workorder  wo   " +
//                            " left join sys_dept dept  on wo.deptcode=dept.code  " +
//                            " ) a   " +
//                            " group by deptcode,fullname  " );
            List<Map<String, Object>> list = handlerService.findBySql
                    (
                            /*" select riqi,  ifnull(jiben,0) jiben  ,ifnull(xunjian,0) xunjian  ,ifnull(fuwu,0) fuwu  ,ifnull(jianweixiu,0) jianweixiu ,  ifnull(weixiu,0) weixiu,  ifnull(weiwai,0) weiwai from (  " +
                            " select DATE_FORMAT(CURDATE(),'%Y-%m-%d') riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 1 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 2 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 3 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 4 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 5 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 6 day ), '%Y-%m-%d' ) riqi) a   " +
                            " left join (  " +
                            " select rq,  " +
                            " sum(jiben) jiben,  " +
                            " sum(xunjian) xunjian,  " +
                            " sum(fuwu) fuwu,  " +
                            " sum(jianweixiu) jianweixiu,  " +
                            " sum(weixiu) weixiu,   " +
                            " sum(weiwai) weiwai   " +
                            " from (  " +
                            " select DATE_FORMAT(createtime,'%Y-%m-%d') rq,  " +
                            " case UDWOTYPE when '基本工单' then 1 else 0 end jiben ,  " +
                            " case UDWOTYPE when '巡点检工单' then 1 else 0 end xunjian ,  " +
                            " case UDWOTYPE when '服务工单' then 1 else 0 end fuwu ,  " +
                            " case UDWOTYPE when '检维修工单' then 1 else 0 end jianweixiu ,  " +
                            " case UDWOTYPE when '维修工单' then 1 else 0 end weixiu ,  " +
                            " case UDWOTYPE when '委外工单' then 1 else 0 end weiwai   " +
                            " from bl_workorder  wo   " +
                            " ) a group by  rq ) b on a.riqi=b.rq");*/
                            " select riqi,  ifnull(yufangxing,0) yufangxing  ,ifnull(weixiu,0) weixiu  ,ifnull(quexian,0) quexian  ,ifnull(dazhongxiu,0) dazhongxiu ,  ifnull(jigai,0) jigai,  ifnull(qiangxiu,0) qiangxiu ,ifnull(zijianzixiu,0) zijianzixiu  from (  " +
                            " select DATE_FORMAT(CURDATE(),'%Y-%m-%d') riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 1 day ), '%Y-%m-%d' ) riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 2 day ), '%Y-%m-%d' ) riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 3 day ), '%Y-%m-%d' ) riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 4 day ), '%Y-%m-%d' ) riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 5 day ), '%Y-%m-%d' ) riqi union " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 6 day ), '%Y-%m-%d' ) riqi) a " +
                            " left join ( " +
                            " select rq, " +
                            " sum(yufangxing) yufangxing, " +
                            " sum(weixiu) weixiu, " +
                            " sum(quexian) quexian, " +
                            " sum(dazhongxiu) dazhongxiu, " +
                            " sum(jigai) jigai, " +
                            " sum(qiangxiu) qiangxiu, " +
                            " sum(zijianzixiu) zijianzixiu " +
                            " from (  " +
                                    " select DATE_FORMAT(createtime,'%Y-%m-%d') rq, " +
                            " case UDWOTYPE when '预防性维护工单' then 1 else 0 end yufangxing , " +
                            " case UDWOTYPE when '维修工单' then 1 else 0 end weixiu , " +
                            " case UDWOTYPE when '缺陷工单' then 1 else 0 end quexian , " +
                            " case UDWOTYPE when '大中修工单' then 1 else 0 end dazhongxiu , " +
                            " case UDWOTYPE when '技改工单' then 1 else 0 end jigai , " +
                            " case UDWOTYPE when '抢修工单' then 1 else 0 end qiangxiu , " +
                            " case UDWOTYPE when '自检自修工单' then 1 else 0 end zijianzixiu  " +
                            "     from bl_workorder  wo   " +
                            "                ) a group by  rq ) b on a.riqi=b.rq");
            List<Object> resuleList1 = new ArrayList<Object>(); // X轴
            List<Object> resuleList2 = new ArrayList<Object>(); // Y轴
            List<Object> resuleList3 = new ArrayList<Object>(); // 预防性维护工单
            List<Object> resuleList4 = new ArrayList<Object>(); // 维修工单
            List<Object> resuleList5 = new ArrayList<Object>(); // 缺陷工单
            List<Object> resuleList6 = new ArrayList<Object>(); // 大中修工单
            List<Object> resuleList7 = new ArrayList<Object>(); // 技改工单
            List<Object> resuleList8 = new ArrayList<Object>(); // 抢修工单
            List<Object> resuleList9 = new ArrayList<Object>(); // 自检自修工单

            Map<String, Object> yMap = new HashMap<String, Object>();

            Map<String, Object> yMap1 = new HashMap<String, Object>();
            Map<String, Object> yMap2= new HashMap<String, Object>();
            Map<String, Object> yMap3 = new HashMap<String, Object>();
            Map<String, Object> yMap4 = new HashMap<String, Object>();
            Map<String, Object> yMap5 = new HashMap<String, Object>();
            Map<String, Object> yMap6 = new HashMap<String, Object>();
            Map<String, Object> yMap7 = new HashMap<String, Object>();
            for (int i = list.size()-1; i >= 0; i--) {
                log.info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+list.get(i).get("riqi"));
                resuleList1.add(list.get(i).get("riqi"));//x轴
                resuleList3.add(list.get(i).get("yufangxing"));
                resuleList4.add(list.get(i).get("weixiu"));
                resuleList5.add(list.get(i).get("quexian"));
                resuleList6.add(list.get(i).get("dazhongxiu"));
                resuleList7.add(list.get(i).get("jigai"));
                resuleList8.add(list.get(i).get("qiangxiu"));
                resuleList9.add(list.get(i).get("zijianzixiu"));
            }
            yMap1.put("data",resuleList3);
            yMap1.put("name","预防性维护工单");
            yMap2.put("data",resuleList4);
            yMap2.put("name","维修工单");
            yMap3.put("data",resuleList5);
            yMap3.put("name","缺陷工单");
            yMap4.put("data",resuleList6);
            yMap4.put("name","大中修工单");
            yMap5.put("data",resuleList7);
            yMap5.put("name","技改工单");
            yMap6.put("data",resuleList8);
            yMap6.put("name","抢修工单");
            yMap7.put("data",resuleList9);
            yMap7.put("name","自检自修工单");
            resuleList2.add(yMap1);
            resuleList2.add(yMap2);
            resuleList2.add(yMap3);
            resuleList2.add(yMap4);
            resuleList2.add(yMap5);
            resuleList2.add(yMap6);
            resuleList2.add(yMap7);
            resultMap.put("msg","操作成功");
            resultMap.put("code","0");
            resultMap1.put("xAxis",resuleList1);
            resultMap1.put("yAxis",resuleList2);
            resultMap.put("data",resultMap1);

            return resultMap;
        } //首页图表 工单物料成本
        if("workorderCost".equals(type)){
            Map<String, Object> resultMap = new HashMap<String, Object>();
            Map<String, Object> resultMap1 = new HashMap<String, Object>();
//            List<Map<String, Object>> list = handlerService.findBySql
//                    (" select deptcode,fullname,sum(cost) cost from (  " +
//                            " select wo.deptcode,dept.fullname,wo.UDWOTYPE,ma.itemnum,ma.ITEMQTY,inven.inprice,(ma.ITEMQTY*inven.inprice) cost from bl_materials ma  " +
//                            " left join bl_workorder wo on wo.wonum=ma.wonum  " +
//                            " left join sys_dept dept on dept.code=wo.deptcode  " +
//                            " left join bl_Invbalance inven on ma.location=inven.warehousenum and ma.itemnum=inven.itemnum  " +
//                            " ) a  group by deptcode,fullname");
            List<Map<String, Object>> list = handlerService.findBySql
                    (
                            " select riqi,ifnull(cost,0) cost from (  " +
                            " select DATE_FORMAT(CURDATE(),'%Y-%m') riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 1 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 2 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 3 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 4 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 5 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 6 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 7 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 8 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 9 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 10 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 11 month ), '%Y-%m' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 12 month ), '%Y-%m' ) riqi   " +
                            " ) a   " +
                            " left join (select rq,sum(cost) cost from (  " +
                            " select DATE_FORMAT(wo.createtime,'%Y-%m') rq,ma.itemnum,ma.ITEMQTY,inven.inprice,(ma.ITEMQTY*inven.inprice) cost from bl_materials ma  " +
                            " left join bl_workorder wo on wo.wonum=ma.wonum  " +
                            " left join bl_Invbalance inven on ma.location=inven.warehousenum and ma.itemnum=inven.itemnum  " +
                            " ) a  group by rq  " +
                            "  ) b on a.riqi=b.rq");

            List<Object> resuleList1 = new ArrayList<Object>(); // X轴
            List<Object> resuleList2 = new ArrayList<Object>(); // Y轴
            List<Object> resuleList3 = new ArrayList<Object>(); // mttr
//            List<Object> resuleList4 = new ArrayList<Object>(); // mtbf
//            List<Object> resuleList5 = new ArrayList<Object>(); // 稼动率

            Map<String, Object> yMap = new HashMap<String, Object>();

            Map<String, Object> yMap1 = new HashMap<String, Object>();
            Map<String, Object> yMap2= new HashMap<String, Object>();
            Map<String, Object> yMap3 = new HashMap<String, Object>();
            for (int i = list.size()-1; i >= 0; i--) {
                resuleList1.add(list.get(i).get("riqi"));//x轴
                resuleList3.add(list.get(i).get("cost"));
//                resuleList4.add(list.get(i).get("mtbf"));
//                resuleList5.add(list.get(i).get("jdv"));
            }
            yMap1.put("data",resuleList3);
            yMap1.put("name","物料成本");
//            yMap2.put("data",resuleList4);
//            yMap2.put("name","MTBF");
//            yMap3.put("data",resuleList5);
//            yMap3.put("name","稼动率");
            resuleList2.add(yMap1);
            resuleList2.add(yMap2);
            resuleList2.add(yMap3);
            resultMap.put("msg","操作成功");
            resultMap.put("code","0");
            resultMap1.put("xAxis",resuleList1);
            resultMap1.put("yAxis",resuleList2);
            resultMap.put("data",resultMap1);

            return resultMap;
        }
        // 首页apm
        if("workorderApm".equals(type)){
            String apm = MapUtils.getString(params,"apmtype");
            Map<String, Object> resultMap = new HashMap<String, Object>();
            Map<String, Object> resultMap1 = new HashMap<String, Object>();
            List<Map<String, Object>> list = handlerService.findBySql
                    (" select a.riqi,ifnull(mttr,0) mttr,ifnull(mtbf,0)mtbf,format(ifnull((jdv),0),2)  jdv  from (  " +
                            " select DATE_FORMAT(CURDATE(),'%Y-%m-%d') riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 1 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 2 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 3 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 4 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 5 day ), '%Y-%m-%d' ) riqi union  " +
                            " select DATE_FORMAT(( CURDATE() - INTERVAL 6 day ), '%Y-%m-%d' ) riqi ) a   " +
                            " left join (select DATE_FORMAT(createtime,'%Y-%m-%d') riqi ,sum(downtime) ,count(*) ,(sum(downtime))/(count(*)) mttr from bl_workorder   " +
                            "  where UDWOTYPE='维修工单'  " +
                            " group by  DATE_FORMAT(createtime,'%Y-%m-%d')) b on a.riqi=b.riqi  " +
                            " left join (  " +
                            " select riqi ,ifnull((24*con2-downtime)/(con1+1),0) mtbf from (  " +
                            " select  DATE_FORMAT(createtime,'%Y-%m-%d') riqi,count(*) con1, sum(downtime) downtime,(select count(*) con2 from bl_asset   " +
                            " )  con2  from bl_workorder  wo   " +
                            " where UDWOTYPE='维修工单'  " +
                            " group by DATE_FORMAT(createtime,'%Y-%m-%d') ) a ) c on a.riqi=c.riqi  " +
                            " left join (select  DATE_FORMAT(createtime,'%Y-%m-%d') riqi,sum(downtime) ,count(*), ((24*count(*))-sum(downtime))/(24*count(*)) jdv from bl_workorder  wo   " +
                            " where UDWOTYPE='维修工单'  " +
                            " group by  DATE_FORMAT(createtime,'%Y-%m-%d') ) d on a.riqi=d.riqi order by a.riqi desc");
            List<Object> resuleList1 = new ArrayList<Object>(); // X轴
            List<Object> resuleList2 = new ArrayList<Object>(); // Y轴
            List<Object> resuleList3 = new ArrayList<Object>(); // mttr
            List<Object> resuleList4 = new ArrayList<Object>(); // mtbf
            List<Object> resuleList5 = new ArrayList<Object>(); // 稼动率

            Map<String, Object> yMap = new HashMap<String, Object>();

            Map<String, Object> yMap1 = new HashMap<String, Object>();
            Map<String, Object> yMap2= new HashMap<String, Object>();
            Map<String, Object> yMap3 = new HashMap<String, Object>();
            for (int i = list.size()-1; i >= 0; i--) {
                resuleList1.add(list.get(i).get("riqi"));//x轴  日期
                resuleList3.add(list.get(i).get("mttr"));
                resuleList4.add(list.get(i).get("mtbf"));
                resuleList5.add(list.get(i).get("jdv"));
            }
            if("mttr".equals(apm)){
                yMap1.put("data",resuleList3);
                yMap1.put("name","MTTR");
                resuleList2.add(yMap1);
            }else if ("mtbf".equals(apm)){
                yMap2.put("data",resuleList4);
                yMap2.put("name","MTBF");
                resuleList2.add(yMap2);
            }else if ("jdv".equals(apm)){
                yMap3.put("data",resuleList5);
                yMap3.put("name","稼动率");
                resuleList2.add(yMap3);
            }
            resultMap.put("msg","操作成功");
            resultMap.put("code","0");
            resultMap1.put("xAxis",resuleList1);
            resultMap1.put("yAxis",resuleList2);
            resultMap.put("data",resultMap1);

            return resultMap;

        }
        return super.operate(app, type, params);

    }

}
